package com.imti.jsonparsedemo;

import android.util.Log;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.json.JSONException;

public class MainActivity extends AppCompatActivity
{

    private static final String TAG = MainActivity.class.getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //initData();

        try
        {
            WeatherResponse<WeatherResult<WeatherRealtime>> result =
                JsonParseUtil.parseCityWeatherByGson(parseJsonFile());

            Log.d(TAG, "查询城市：" + result.getReason());
            Log.d(TAG, "天气：" + result.getResult().getRealtime().getInfo());
            Log.d(TAG, "温度：" + result.getResult().getRealtime().getTemperature());
            Log.d(TAG, "湿度：" + result.getResult().getRealtime().getHumidity());
            Log.d(TAG, "风向：" + result.getResult().getRealtime().getDirect());
            Log.d(TAG, "风力：" + result.getResult().getRealtime().getPower());
            Log.d(TAG, "空气质量：" + result.getResult().getRealtime().getAqi());

        } catch (IOException e)
        {
            e.printStackTrace();
        }

    }

    private void initData() throws IOException, JSONException
    {
        WeatherResult result =
            JsonParseUtil.parseCityWeather(parseJsonFile());

//        Log.d(TAG, "查询城市：" + result.getCity());
//        Log.d(TAG, "天气：" + result.getRealtime().getInfo());
//        Log.d(TAG, "温度：" + result.getRealtime().getTemperature());
//        Log.d(TAG, "湿度：" + result.getRealtime().getHumidity());
//        Log.d(TAG, "风向：" + result.getRealtime().getDirect());
//        Log.d(TAG, "风力：" + result.getRealtime().getPower());
//        Log.d(TAG, "空气质量：" + result.getRealtime().getAqi());
    }

    private String parseJsonFile() throws IOException
    {
        InputStream is = getAssets().open("weather.json");

        BufferedReader isr = new BufferedReader(new InputStreamReader(is));

        String s = "";
        StringBuffer sb = new StringBuffer();
        while ((s = isr.readLine()) != null)
        {
            sb.append(s);
        }

        return sb.toString();
    }
}